package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.datas.RecommendHostSongsDatas;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by quyan on 15/10/22.
 */
public class RecommendHostSongAdapter extends BaseAdapter {
    private List<RecommendHostSongsDatas.ContentEntity> datas;
    private Context context;
    private ImageLoader imageLoader;
    private List<RecommendHostSongsDatas.ContentEntity.SongListEntity> songListEntities;

    public RecommendHostSongAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();
    }
    public void addData(List<RecommendHostSongsDatas.ContentEntity> contentEntities){
        datas = contentEntities;
        songListEntities = datas.get(0).getSong_list();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return songListEntities != null && songListEntities.size() > 0 ? songListEntities.size() : 0;
    }

    @Override
    public RecommendHostSongsDatas.ContentEntity.SongListEntity getItem(int position) {
        return songListEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_recom_listview,null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.hostSongImage);
            holder.songName = (TextView) convertView.findViewById(R.id.songName);
            holder.autherName = (TextView) convertView.findViewById(R.id.autherName);
            holder.wordText = (TextView) convertView.findViewById(R.id.wordText);
            holder.imageicon = (ImageView) convertView.findViewById(R.id.songImageList);
            holder.songHotItem = (CardView) convertView.findViewById(R.id.songHotItem);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }


        //根据下标获取数据
        RecommendHostSongsDatas.ContentEntity.SongListEntity songListEntity =
                getItem(position);
        holder.songName.setText(songListEntity.getTitle());
        holder.autherName.setText(songListEntity.getAuthor());
        holder.wordText.setText(songListEntity.getRecommend_reason());
        String image = songListEntity.getPic_small();
        Log.i("cxh","image" + image + " ");
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.imageView,
                R.mipmap.default_app_rec,R.mipmap.default_app_rec);
        imageLoader.get(image,listener);
        holder.imageicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog myDialog = new AlertDialog.Builder(context).create();
                myDialog.show();
                Window window = myDialog.getWindow();
                window.setContentView(R.layout.my_dialog);

                Button love = (Button) window.findViewById(R.id.my_dialog_love);
                // 收藏的监听
                love.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                // 分享的监听
                Button share = (Button) window.findViewById(R.id.my_dialog_share);
                share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

                Button download = (Button) window.findViewById(R.id.my_dialog_download);
                download.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

                Button cancel = (Button) window.findViewById(R.id.my_dialog_cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                    }
                });
                window.setLayout(700, 500);
            }


        });

        holder.songHotItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (RecommendHostSongsDatas.ContentEntity.SongListEntity entity : songListEntities) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_small());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });



        return convertView;
    }

    public class ViewHolder{
        private ImageView imageView,imageicon;
        private TextView songName,autherName,wordText;
        private CardView songHotItem;
    }
}
